package com.example.utils;

public class HexString {

  private HexString() {
    // Utility class.
  }

  private final static char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray();

  public static String bytesToHex(byte[] bytes) {
    if (bytes == null || bytes.length == 0) return "";
    char[] hexChars = new char[bytes.length * 2];

    for (int j = 0; j < bytes.length; j++) {
      int v = bytes[j] & 0xFF;
      hexChars[j * 2] = HEX_ARRAY[v >>> 4];
      hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F];
    }

    return new String(hexChars);
  }

  /**
   * 16进制字符串转byte[]
   *
   * @param hexRepresentation
   * @return
   */
  public static byte[] hexToBytes(String hexRepresentation) {
    if (hexRepresentation.length() % 2 == 1) {
      throw new IllegalArgumentException("hexToBytes requires an even-length String parameter");
    }

    int len = hexRepresentation.length();
    byte[] data = new byte[len / 2];

    for (int i = 0; i < len; i += 2) {
      data[i / 2] = (byte) ((Character.digit(hexRepresentation.charAt(i), 16) << 4) + Character.digit(
          hexRepresentation.charAt(i + 1), 16));
    }

    return data;
  }
}
